package com.henu.zmb.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.henu.R;
import com.henu.zmb.entity.GoodsClass;
import java.util.ArrayList;

/**
 * Created by zhumengbo on 2017/5/16.
 */

public class GoodsClass_adapter extends BaseAdapter{
    ArrayList<GoodsClass> list = new ArrayList<GoodsClass>();
    LayoutInflater inflater;// 布局填充器
    public GoodsClass_adapter(Context context) {
        inflater = LayoutInflater.from(context);// 获取当前上下文的布局填充器
    }
    // 添加数据
    public void addAllData(ArrayList<GoodsClass> templist){
        list.clear();
        list.addAll(templist);
    }
    public void addData(ArrayList<GoodsClass> templist){
        list.addAll(templist);
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        // 不需要每次更新都获取控件，只在第一次时进行
        if(convertView == null){
            convertView = inflater.inflate(R.layout.activity_goodsclass_adapter, null);//把 xml文件转化为view视图
            holder = new ViewHolder();
            holder.image = (ImageView)convertView.findViewById(R.id.agca_ivimage);
            holder.name = (TextView)convertView.findViewById(R.id.agca_tvName);
          //  holder.mess = (TextView)convertView.findViewById(R.id.agca_tvmess);
            convertView.setTag(holder);// 将holder放入converView中
        }else{
            holder = (ViewHolder) convertView.getTag();// 获取之前存放的holder
        }
        GoodsClass g = list.get(position);
        //设置当前控件数据（position）
        holder.image.setBackgroundResource(g.getGoodsClass_Image());
        holder.name.setText(g.getGoodsClass_Name());
       // holder.mess.setText(g.getGoodsClasss_Message());
        return convertView;
    }

    class ViewHolder {
        ImageView image;
        TextView name;
        //TextView mess;
    }
}
